merge 1:1 RELEASE_YR ID_VAR FILE_TYPE CAR_CPACT using Residential CAR.dta, nogen  update replace 

************************************
*school
************************************

gen school_enroll=0
replace school_enroll=1 if D32Q1A4==1
replace school_enroll=2 if D32Q1A5==1 | D32Q1A6==1

gen school_conduct=0
replace school_conduct=1 if D32Q7A3==1 | D32Q7A4==1
replace school_conduct=2 if D32Q7A5==1 

gen school_attend=0
replace school_attend=1 if D32Q9A3==1 | D32Q9A4==1
replace school_attend=2 if D32Q9A5==1


gen school_perform=0
replace school_perform=1 if D32Q10A3==1 
replace school_perform=2 if D32Q10A4==1 | D32Q10A5==1

************************************
*peers
************************************

* friends - codes anti-social friends as 1
gen friends_past = 0
replace friends_past=1 if D61Q2A1==1 | D61Q2A3==1
replace friends_past=2 if D61Q2A4==1

gen friends_curr = 0
replace friends_curr=1 if D62Q3A1==1 | D62Q3A3==1
replace friends_curr=2 if D62Q3A4==1

* friends - codes anti-social friends as 2
gen friends_past1 = 0
replace friends_past1=1 if D61Q2A1==1 
replace friends_past1=2 if D61Q2A4==1 | D61Q2A3==1

gen friends_curr1 = 0
replace friends_curr1=1 if D62Q3A1==1 
replace friends_curr1=2 if D62Q3A4==1 | D62Q3A3==1


************************************
*family
************************************

* out of home placements
gen DCF=0
replace DCF=2 if  D71Q1A2==1 | D71Q1A3==1 | D71Q1A4==1

*run-aways, kicked out of home
gen run_kickout=0
replace run_kickout=2 if D71Q2A2 ==1 | D71Q2A3 ==1 | D71Q2A4 ==1 | D71Q2A5 ==1 

*history of jail/prison, anyone involved with the household for at least 3 mos
gen pris_his=0
replace pris_his=2 if   D71Q4A2==1 | D71Q4A3==1 | D71Q4A4==1 | D71Q4A5==1 | D71Q4A6==1 

* anyone involved in the household currently in jail/prison
gen pris_curr=0
replace pris_curr=2 if D72Q3A6==1 | D72Q3A2==1 | D72Q3A3==1 | D72Q3A4==1 | D72Q3A5==1 

*problem history of parents currently involved in household
gen parent_prob=0
replace parent_prob=2 if D72Q4A2==1 | D72Q4A3==1 | D72Q4A4==1 | D72Q4A5==1 | D72Q4A6==1

*parental authority
gen parent_auth=0
replace parent_auth=1 if D72Q13A2==1
replace parent_auth=2 if D72Q13A3==1

************************************
*drugs and alcohol
************************************

*alcohol past
gen alc_past=0
replace alc_past=1 if D81Q1A2==1
replace alc_past=2 if D81Q1A3==1 | D81Q1A4==1 | D81Q1A5==1 | D81Q1A6==1 | D81Q1A7==1 | D81Q1A8==1 | D81Q1A9==1

*drug past
gen drug_past=0
replace drug_past=1 if D81Q2A2==1
replace drug_past=2 if D81Q2A3==1 | D81Q2A4==1 | D81Q2A5==1 | D81Q2A6==1 | D81Q2A7==1 | D81Q2A8==1 | D81Q2A9==1

*alcohol curr
gen alc_curr=0
replace alc_curr=1 if D82Q1A2==1
replace alc_curr=2 if D82Q1A3==1 | D82Q1A4==1 | D82Q1A5==1 | D82Q1A6==1 | D82Q1A7==1 | D82Q1A8==1 | D82Q1A9==1

*drug curr
gen drug_curr=0
replace drug_curr=1 if D82Q2A2==1
replace drug_curr=2 if D82Q2A3==1 | D82Q2A4==1 | D82Q2A5==1 | D82Q2A6==1 | D82Q2A7==1 | D82Q2A8==1 | D82Q2A9==1


************************************
*mental health
************************************
 
gen mental_health=2
replace mental_health=0 if D91Q7A1==1

************************************
*abuse and neglect
************************************

*physical abuse or violence
gen phys_abuse=0
replace phys_abuse=2 if D91Q2A2==1 | D91Q2A3==1 | D91Q2A4==1 |  D91Q2A5==1 | D91Q2A6==1

*witnessing violence
gen witness_viol=0
replace witness_viol=1 if D91Q3A4==1
replace witness_viol=2 if D91Q3A2==1 | D91Q3A3==1  |  D91Q3A5==1 

*sexual abuse rape
gen sex_abuse=0
replace sex_abuse=2 if D91Q4A2==1 | D91Q4A3==1   

*neglect
gen neglect=0
replace neglect=2 if D91Q5A2==1 


************************************
*bonus
************************************
*chronic umemployment in parents
gen par_unemp=0
replace par_unemp=1 if D72Q4A6==1

*family member killed by violence
gen viol_death_fam=0
replace viol_death_fam=1 if D91Q3A5==1
la var viol_death_fam "Violent death in family" 

*suicidal
gen suicidal=0
replace suicidal=1 if D91Q1A2==1 | D91Q1A6==1 | D91Q1A3==1 | D91Q1A4==1 | D91Q1A5==1 | D91Q1A7==1

*anger
gen anger=0
replace anger=2 if D91Q8A3==1 | D91Q8A4==1 

*depression and anxiety
gen depress_anx=0
replace depress_anx=2 if D91Q9A3==1 | D91Q9A4==1 

*somatic complaints
gen somatic=0
replace somatic=2 if D91Q10A3==1 | D91Q10A4==1 | D91Q10A2==1 

*hallucinations
gen halluc=0
replace halluc=2 if D91Q11A3==1 | D91Q11A2==1 

*traumatic event
gen trauma=0
replace trauma=2 if D91Q12A3==1 | D91Q12A2==1


************************************
*new factors
************************************
* put in missing values
foreach x in school_enroll school_conduct school_attend school_perform friends_past friends_curr friends_past1 friends_curr1 DCF run_kickout pris_his pris_curr parent_prob parent_auth alc_past drug_past alc_curr drug_curr mental_health phys_abuse witness_viol sex_abuse neglect par_unemp viol_death_fam suicidal anger depress_anx somatic halluc trauma {
replace `x'=. if chscr==.
}


global indicators school_enroll school_conduct school_attend school_perform friends_past friends_curr1 DCF run_kickout pris_his pris_curr parent_prob parent_auth alc_past drug_past alc_curr drug_curr mental_health phys_abuse witness_viol sex_abuse neglect 

*build factors, just social history
factor $indicators, pcf
rotate, blanks(.3)
predict drugsalc abuse_trauma schoolprob parents_prison wildcrowd kickout
estout e(L)


************************************
* summarizing
************************************

* dropout
gen dropout=0
replace dropout=1 if D32Q1A5==1 | D32Q1A6==1
la var dropout "Dropped out or expelled"

*alcohol curr
gen alc=0
replace alc=1 if D82Q1A2==1 | D82Q1A3==1 | D82Q1A4==1 | D82Q1A5==1 | D82Q1A6==1 | D82Q1A7==1 | D82Q1A8==1 | D82Q1A9==1
la var alc "Drinks alcohol"

*drug curr
gen drug=0
replace drug=1 if  D82Q2A2==1 | D82Q2A3==1 | D82Q2A4==1 | D82Q2A5==1 | D82Q2A6==1 | D82Q2A7==1 | D82Q2A8==1 | D82Q2A9==1
la var drug "Does drugs" 

* family member in prison
gen pris=1
replace pris=0 if D72Q3A1==1 & D71Q4A1==1 
la var pris "Family member has been incarcerated"


* parents incarcerated
gen parpris=(D71Q4A2==1 | D71Q4A3==1 | D72Q3A2==1 | D72Q3A3==1) 
la var parpris "Parent has been incarcerated"


* parents with drug/alcohol problems
gen pardrug=(D72Q4A2==1 | D72Q4A3==1) 
la var pardrug "Parents w/drug, alc. prob"

* gang affiliate
gen gangz = 0
replace gangz= 1 if D61Q2A4==1 |  D62Q3A4==1 
la var gangz "Known gang affiliate"


*physical abuse or violence
gen abused=0
replace abused=1 if D91Q2A2==1 | D91Q2A3==1 | D91Q2A4==1 |  D91Q2A5==1 | D91Q2A6==1
la var abused "Physically abused"

*sexual abuse rape
gen abuse_sex=0
replace abuse_sex=1 if D91Q4A2==1 | D91Q4A3==1  
la var abuse_sex "Sexually abused"


* out of home placements
gen DCF2=0
replace DCF2=1 if  D71Q1A2==1 | D71Q1A3==1 | D71Q1A4==1
la var DCF2 "Been in foster care/shelter"

*run-aways, kicked out of home
gen run_kickout2=0
replace run_kickout2=1 if D71Q2A2 ==1 | D71Q2A3 ==1 | D71Q2A4 ==1 | D71Q2A5 ==1 
la var run_kickout2 "Ran away or was kicked out"

*peer problem indicator
gen peerz=0
replace peerz=1 if D62Q3A4==1 | D62Q3A3==1 | D61Q2A4==1

*previously committed
gen prevcom=(D1Q10A2==1)
la var prevcom "Previously committed"

************************************
* criminal history indicators
************************************

* age at first offense
gen agfrof=0
replace agfrof=1 if D1Q1A2==1 
replace agfrof=2 if D1Q1A3==1
replace agfrof=3 if D1Q1A4==1
replace agfrof=4 if D1Q1A5==1

* first offense under age 14
gen fr14und=0
replace fr14und=1 if D1Q1A4==1
replace fr14und=1 if D1Q1A5==1
la var fr14und "First offense under 14yrs"

* first offense under age 12
gen fr12und=0
replace fr12und=1 if D1Q1A5==1
la var fr12und "First offense under 12yrs"

* misdemeanor referrals
gen misref=0
replace misref=1 if D1Q2A2==1 
replace misref=2 if D1Q2A3==1
replace misref=3 if D1Q2A4==1

* felony referrals
gen felref=0
replace felref=1 if D1Q3A2==1 
replace felref=2 if D1Q3A3==1
replace felref=3 if D1Q3A4==1

* weapon referrals
gen weapref=0
replace weapref=1 if D1Q4A2==1

* against person misdemeanor referrals
gen apmis=0
replace apmis=1 if D1Q5A2==1 
replace apmis=2 if D1Q5A3==1

* against person felony referrals
gen apfel=0
replace apfel=1 if D1Q6A2==1 
replace apfel=2 if D1Q6A3==1

* sexual misconduct
gen sexmis=0
replace sexmis=1 if D1Q7A2==1 
replace sexmis=2 if D1Q7A3==1

* felony sex offense
gen sexfel=0
replace sexfel=1 if D1Q8A2==1 
replace sexfel=2 if D1Q8A3==1

* detention
gen deten=0
replace deten=1 if D1Q9A2==1 
replace deten=2 if D1Q9A3==1
replace deten=3 if D1Q9A4==1


* residential 
gen resident=0
replace resident=1 if D1Q10A2==1 
replace resident=2 if D1Q10A3==1

* escapes 
gen escap=0
replace escap=1 if D1Q11A2==1 
replace escap=2 if D1Q11A3==1

* failure to appear 
gen failap=0
replace failap=1 if D1Q12A2==1 
replace failap=2 if D1Q12A3==1

*build factors, criminal history
*factor $chind, pcf
*factor $indicators, pcf
*rotate, blanks(.3) 
*predict sercrime cascrime rape 

foreach x in dropout alc drug pris gangz abused abuse_sex DCF2 run_kickout2 peerz agfrof misref felref weapref apmis apfel sexmis sexfel deten resident escap failap{
replace `x'=. if chscr==.
}


***************************************
***** build own factors
***************************************

gen h_drugalc=(alc_past +drug_past +alc_curr +drug_curr)/4
gen h_schoolprob=(school_conduct +school_attend +school_perform)/3
gen h_famprob=(DCF +run_kickout +pris_his +pris_curr +parent_prob +parent_auth+ neglect)/7
gen h_trauma=(phys_abuse +witness_viol +sex_abuse)/3
gen h_peers=(friends_past + friends_curr1)/2

gen h_famprob1=(run_kickout +pris_his +pris_curr +parent_prob +parent_auth)/5
gen h_trauma1=(DCF+phys_abuse +witness_viol +sex_abuse+neglect)/3

***************************************
***** noncogs summary
***************************************

* Optimism
gen optN=(D10Q3A4==1 | D10Q3A3==1)
la var optN "Low/no hope or aspiration"

* Impulsiveness
gen impN=(D10Q4A3==1 | D10Q4A4==1)
la var impN "Impulsive or highly impulsive"


* Empathy, remorse
gen empathN=(D10Q6A3==1)
la var empathN "Lacks empathy for victim"

* Respect for property of others
gen resp_propN=(D10Q7A4==1)
la var resp_propN "Lacks respect for other's property"

* Tolerance for frustration
gen tolfrustN=.
replace tolfrustN=(D11Q1A3==1)
la var tolfrustN "Low tolerance for frustration"

* Hostile interpretation of other's actions
gen hostintN=(D11Q2a2==1 | D11Q2A3==1)
la var hostintN "Hostile interp. of other's actions"

* Belief in verbal aggression
gen verb_aggN=(D11Q3A3==1)
la var verb_aggN "Belief in verb. agg. to solve conflict"

* Belief in physical aggression
gen phys_aggN=(D11Q4A3==1 | D11Q4A4==1)
la var phys_aggN "Belief in phys. agg. to solve conflict"
gen phys_agg2N=(D11Q4A4==1)
la var phys_agg2N "Belief in phys. agg. to solve conflict"

* Goal setting
gen goal_setN=(D12Q2a3==1 | D12Q2A4==1)
la var goal_setN "No/unrealistic goals"


* Situational perception
gen sit_percN=(D12Q4a2==1 | D12Q4A1==1)
la var sit_percN "Poor situational perception"


* Social skills
gen soc_skillN=(D12Q5a2==1 | D12Q5A1==1)
la var soc_skillN "Poor social skills"


* Dealing with difficult emotions
gen diff_emotN=( D12Q7a2==1 | D12Q7A1==1)
la var diff_emotN "Poor emotional skills"


